package xmlcurl

import (
	"encoding/xml"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

const (
	sectionDirectory     = "directory"
	sectionConfiguration = "configuration"
	sectionDialplan      = "dialplan"
	continueTrue         = "true"
	breakOnFalse         = "on-false"
	breakOnTrue          = "on-true"
	breakNever           = "never"
	breakAlways          = "always"
	regexAny             = "any"
	regexAll             = "all"
	regexXor             = "xor"
	postPurpose          = "purpose"
	postGateways         = "gateways"
	postNetworkList      = "network-list"
	postUser             = "user"
	postDomain           = "domain"
	postGroup            = "group"
	postKeyValue         = "key_value"
	postTagName          = "tag_name"
	postCallerContext    = "Caller-Context"
	postProfile          = "profile"
	postCCQueue          = "CC-Queue"
	//Header               = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>` + "\n"
)

type DebugLog struct {
	Log []string
}

func (d *DebugLog) AddLogLine(line string) {
	d.Log = append(d.Log, line)
}

type Document struct {
	XMLName xml.Name  `xml:"document"`
	Type    string    `xml:"type,attr"`
	Section []Section `xml:"section"`
	Comment string    `xml:",comment"`
}

func (document *Document) AddSection(section Section) {
	document.Section = append(document.Section, section)
}

type Section struct {
	XMLName         xml.Name            `xml:"section,omitempty" json:"section,omitempty"`
	Attrname        string              `xml:"name,attr,omitempty"  json:",omitempty"`
	Attrdescription string              `xml:"description,attr,omitempty"  json:",omitempty"`
	X_NOPRE_PROCESS []*X_NO_PRE_PROCESS `xml:"X-NO-PRE-PROCESS,omitempty" json:"X-NO-PRE-PROCESS,omitempty"`
	Configuration   []*Configuration    `xml:"configuration,omitempty" json:"configuration,omitempty"`
	Context         []*Context          `xml:"context,omitempty" json:"context,omitempty"`
	Domain          []*Domain           `xml:"domain,omitempty" json:"domain,omitempty"`
	Result          []Result            `xml:"result,omitempty"`
	Language        []*Language         `xml:"language,omitempty" json:"language,omitempty"`
}

func (section *Section) addResult(status string) {
	section.Result = append(section.Result, Result{
		Status: status,
	})
}

type Result struct {
	Status string `xml:"status,attr"`
}

func NotFound(post map[string]string) []byte {
	document := NewDocument()
	if post != nil {
		document.Comment = fmt.Sprintf("section: %s, keyValue: %s, purpose: %s, user: %s, domain: %s, profile: %s, context: %s, tagName: %s.",
			post["section"], post[postKeyValue], post[postPurpose], post[postUser], post[postDomain], post[postProfile], post[postCallerContext], post[postTagName])
	}
	section := NewSection("result")
	section.addResult("not found")
	document.AddSection(section)

	res, ok := XmlToByte(document)
	if !ok {
		return []byte{}
	}
	return res
}

func XmlToByte(value ...interface{}) ([]byte, bool) {
	output, err := xml.MarshalIndent(value, "", "  ")
	if err != nil {
		logx.Errorf("%+v", err)
		return []byte{}, false
	}
	output = append(output, '\n')
	return output, true
}

func NewDocument() Document {
	return Document{Type: "freeswitch/xml"}
}

func NewSection(name string) Section {
	return Section{XMLName: xml.Name{}, Attrname: name}
}

func PrepareDocument(document Document) []byte {
	res, ok := XmlToByte(document)
	if !ok {
		return NotFound(nil)
	}
	return res
}
